﻿#include "MaterialSplitVectorNodeViewModel.h"
#include "MaterialSplitVectorNodeModel.h"

#include "MaterialGraph/MaterialGraphViewModel.h"
#include "MaterialGraph/Pin/MaterialGraphPinViewModel.h"

#include "MaterialGraph/UndoRedo/MaterialEditOperation.h"

void RMaterialSplitVectorNodeViewModel::BindModel(TSharedObjectPtr<RMaterialGraphNodeModel> InModel, TSharedObjectPtr<RMaterialGraphViewModel> InGraph)
{
    Super::BindModel(InModel, InGraph);

    SplitVectorModel = InModel;

    //assgin line index
    //line 0 is the title
    OutputViewModels[0]->SetLineIndex(1);
    OutputViewModels[1]->SetLineIndex(2);
    OutputViewModels[2]->SetLineIndex(3);
    //line 4 is the check box for Is2D
    InputViewModels[0]->SetLineIndex(5);
    InputViewModels[1]->SetLineIndex(5);    

}

int64_t RMaterialSplitVectorNodeViewModel::GetLineNum() const
{
    if(IsSplit2DVector())
    {
        if(InputViewModels[1]->HasValidLink())
        {
            return 6;
        }
        return 8;
    }
    else
    {
        if(InputViewModels[0]->HasValidLink())
        {
            return 6;
        }
        return 9;
    }
}

CString RMaterialSplitVectorNodeViewModel::GetInputPinName(int64_t Index) const
{
    switch (Index)
    {
    case 0:
        return U"三维向量";
    case 1:
        return U"二维向量";
    default:
        return U"";
    }
}

CString RMaterialSplitVectorNodeViewModel::GetOutputPinName(int64_t Index) const
{
    switch (Index)
    {
    case 0:
        return U"X(红色)";
    case 1:
        return U"Y(绿色)";
    case 2:
        return U"Z(蓝色)";
    default:
        return U"";
    }
}

bool RMaterialSplitVectorNodeViewModel::IsSplit2DVector() const
{
    return SplitVectorModel->IsSplit2D();
}

void RMaterialSplitVectorNodeViewModel::SetSplit2DVector(bool bInSplit2DVector)
{
    bool bOldSplit2D = IsSplit2DVector();
    auto Operation = NewObject<RMaterialFunctionOperation>();
    
    auto Model=SplitVectorModel;
    Operation->UndoFunction.Bind([Model, bOldSplit2D](TSharedObjectPtr<RMaterialGraphModel>) {
        Model->SetSplit2D(bOldSplit2D);
    });
    Operation->RedoFunction.Bind([Model, bInSplit2DVector](TSharedObjectPtr<RMaterialGraphModel>) {
        Model->SetSplit2D(bInSplit2DVector);
    });

    GraphViewModel->RedoOperation(Operation);
    GraphViewModel->AddOperation(Operation);
}
